package com.cxd.performance.service.impl;


import com.alibaba.excel.EasyExcel;
import com.cxd.performance.domain.PerformanceAmountBaseInfo;
import com.cxd.performance.domain.PerformanceBaseInfo;
import com.cxd.performance.listener.PerformanceAmountAnalysisEventListener;
import com.cxd.performance.listener.PerformanceScoreAnalysisEventListener;
import com.cxd.performance.service.MailSenderService;
import com.cxd.performance.service.SendPerformanceMailService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StopWatch;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;

@Slf4j
@Service
public class SendPerformanceMailServiceImpl implements SendPerformanceMailService {

    @Autowired
    private MailSenderService mailSenderService;

    @Override
    public void sendPerformanceAmountMail(String filePath) {
        StopWatch sw = new StopWatch();
        sw.start("**************************发送绩效评分**********************");
        EasyExcel.read(filePath, PerformanceAmountBaseInfo.class, new PerformanceAmountAnalysisEventListener(mailSenderService)).headRowNumber(1).sheet("核算底稿").doRead();
        sw.stop();
        log.info(sw.prettyPrint());
    }

    @Override
    public void generatePerformanceScore(MultipartFile file) {
        // 1.获取上传文件输入流
        try (InputStream inputStream = file.getInputStream()) {
            EasyExcel.read(inputStream, PerformanceBaseInfo.class, new PerformanceScoreAnalysisEventListener()).headRowNumber(1).sheet(1).doRead();
        } catch (Exception e) {
            log.error("解析数据发生异常", e);
            throw new RuntimeException("解析数据发生异常：", e);
        }
    }

    @Override
    public void generatePerformanceScoreByFilePath(String filePath) {
        EasyExcel.read(filePath, PerformanceBaseInfo.class, new PerformanceScoreAnalysisEventListener()).headRowNumber(1).sheet(0).doRead();
    }
}
